import java.util.Scanner;

public class Client {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Sys sys = new Sys();//管理类只能实例化一次
        System.out.println("*************欢迎使用学生管理系统*************");
        //登陆功能
        boolean flag = login(input, sys);


        while (flag) {
            System.out.println("1）添加学生");
            System.out.println("2）根据学生姓名查询学生");
            System.out.println("3）根据学号删除学生");
            System.out.println("4）查询所有学生信息");
            System.out.println("5）根据学生年龄排序");
            System.out.println("6）统计学生的人数");
            System.out.println("7）退出程序");
            int choose = input.nextInt();

            switch (choose) {
                case 1:
                    //门面的添加学生功能
                    addStudent(input, sys);
                    break;
                case 2:
                    //门面的根据学生姓名查找学生
                    findStudentByName(input, sys);
                    break;
                case 3:
                    //根据学号删除学生
                    removeStuById(input, sys);
                    break;
                case 4:
                    //查找所有学生
                    printStudent(sys.findAll());
                    break;
                case 5:
                    //根据年龄排序
                    printStudent(sys.sortByAge());

                    break;
                case 6:
                    //统计学生人数
                    getStudentCount(sys);
                    break;
                case 7:
                    System.out.println("谢谢使用，再见");
                    return;
                default:
                    System.out.println("输入有误，请重新输入！");
            }
            System.out.println();
        }
    }

    private static void getStudentCount(Sys sys) {
        int count = sys.getCount();
        System.out.println("已知入档学生人数：" + count);
    }

    private static void removeStuById(Scanner input, Sys sys) {
        System.out.println("请输入要删除的学号");
        int stuId = input.nextInt();
        int i = sys.removeById(stuId);
        if (i < 0) {
            System.out.println("删除失败！未找到对应的学生");
        } else {
            System.out.println("删除成功!");
        }
    }

    private static void findStudentByName(Scanner input, Sys sys) {
        String name;
        System.out.println("请输入姓名");
        name = input.next();
        printStudent(sys.findByName(name));
    }

    private static void addStudent(Scanner input, Sys sys) {
        String name;
        System.out.println("请输入电话");
        String phone=input.next();
        //验证号码是否存在
        Student studentByPhone=sys.findStudentByPhone(phone);
        if (studentByPhone != null){
            System.out.println("这个号码已被占用！");
            return;
        }
        System.out.println("请输入姓名");
        name=input.next();
        System.out.println("请输入年龄");
        int age=input.nextInt();
        System.out.println("请输入性别");
        char gender=input.next().charAt(0);
        System.out.println("请输入密码");
        String password=input.next();

        //将用户输入的数据构建为学生对象
        Student student=new Student(name,age,gender,phone,password);

        //将对象传递给后台实现插入功能
        boolean add= sys.add(student);
        System.out.println(add ? "添加学生成功!":"添加学生失败!");
    }

    private static boolean login(Scanner input, Sys sys) {
        int against = 3; //剩余尝试次数
        do {
            System.out.print("请输入用户名：");
            String loginName = input.next();

            System.out.print("请输入密码：");
            String loginPwd = input.next();

            String nickName = sys.login(loginName, loginPwd);
            //System.out.println(nickName != null ? "欢迎" + nickName + "登陆！" : "用户名或密码错误！");

            if (nickName == null) {
                against--;
                if (against < 1) {
                    System.out.println("失败次数过多，账户已被锁定");
                }else{
                    System.out.println("用户名或密码错误，剩余尝试次数" + against);
                }
            } else {
                System.out.println("登陆成功，欢迎" + nickName);
                System.out.println();
                return true;
            }
        } while (against > 0);
        return false;
    }

    /**
     * 遍历对象数组
     * @param students
     */
    private static void printStudent(Student[] students) {
        System.out.println("学号\t\t|姓名\t\t|年龄\t\t|性别\t\t|电话");
        for (int i = 0; i < students.length; i++) {
            Student s = students[i];
            String str = String.format("%d\t\t|%s\t\t|%d\t\t|%s\t\t|%s",s.getStuId(),s.getName(),s.getAge(),s.getGender(),s.getPhone());
            System.out.println(str);
            //System.out.println(s.getStuId() + "\t" + s.getName() + "\t" + s.getAge() + "\t" + s.getGender() + "\t" + s.getPhone());
        }
    }
}
